<template>
  <div class="user">
    <!-- 按钮 -->
     <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%" size="small">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="username" label="用户名" width="100"> </el-table-column>
      <el-table-column prop="password" label="密码" width="100"> </el-table-column>
      <el-table-column prop="telephone" label="电话" width="100"> </el-table-column>
      <el-table-column prop="gender" label="性别" width="100"> </el-table-column>
      <el-table-column prop="realName" label="真实姓名" width="100"> </el-table-column>
      <el-table-column prop="photo" label="照片" width="100"> </el-table-column>
      <el-table-column prop="status" label="状态" width="100"> </el-table-column>
      <el-table-column prop="role_id" label="角色id" width="100"> </el-table-column>
      <el-table-column label="操作" width="180" align="center"> 
        <template v-slot="scope">
          <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
          <el-button size="small" type="primary" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框 -->
    
    <el-dialog
    class="customer_model"
      title="添加用户"
      :visible.sync="dialogVisible"
      width="60%">
      {{form}}
            <el-form :model="form" label-width="100px">
            <el-form-item label="序号" >
              <el-input v-model="form.index"></el-input>
             </el-form-item>
             <el-form-item label="用户名" >
              <el-input v-model="form.username"></el-input>
             </el-form-item> 
            <el-form-item label="密码" >
             <el-input v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="电话" >
             <el-input v-model="form.telephone"></el-input>
            </el-form-item>
            <el-form-item label="性别" >
             <el-input v-model="form.gender"></el-input>
            </el-form-item>
            <el-form-item label="真实姓名" >
             <el-input v-model="form.realName"></el-input>
            </el-form-item>
             <el-form-item label="照片" >
             <el-input v-model="form.photo"></el-input>
            </el-form-item>
             <el-form-item label="状态" >
             <el-input v-model="form.status"></el-input>
            </el-form-item>
            <el-form-item label="角色ID" >
             <el-input v-model="form.role_id"></el-input>
            </el-form-item>
          </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="save(form)" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
//导入request ,浏览器提供给我们的用于与后台进行交互的工具 api
import {get,post}  from '@/utils/request'
export default {
  data (){
    return {
      form:{},
      list:[],
      dialogVisible:false
    }
  },
  created(){
    this.loadData();
  },
  methods:{
       save(){
        let url = "/user/saveOrUpdate"
        post(url,this.form).then((response)=>{
          //1.模态框关闭
          this.dialogVisible = false;
          //2.提示
          this.$message({type:"success",message:response.message});

          //3.刷新
          this.loadData();
        }
        )
      },
    //删除
    del(row){
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
       }).then(()=>{
        let url = "/user/deleteById"
        get(url,{id:row.id}).then((response)=>{
          //1.提示
          this.$message({type:"success",message:response.message});
          //2.刷新
          this.loadData();
        })
       })
      },
    //编辑
    edit(row){
        this.form = row;
        this.dialogVisible = true;
    },
    //加载数据
    loadData(){
      let url = "/user/findAll";
      get(url).then( (response)=>{
        //response就是后端返回给我们的Message{status,message,data,timestamp}
        this.list = response.data;
      } );

    },
    toAdd(){
      this.dialogVisible = true;
    },
    data(){
      return{
        list:[],
        dialogVisible:false
      }
    },
    toDelete(){
     this.dialogVisible = true;
    },
    toSelect(){
    this.dialogVisible = true;
    }

  }
}
</script>



    
  